//
//  TitleView.m
//  ImGuider
//
//  Created by 王鹏宇 on 2017/4/18.
//  Copyright © 2017年 imguider. All rights reserved.
//

#import "TitleView.h"

@implementation TitleView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self createTitleLable];
        [self addSubview:self.searchView];
    }
    return self;
}

- (void)setTitle:(NSString *)title {
    _title = title;
    _titleLabel.text = title;
}
- (void)createTitleLable {
    _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0,kScreenWidth - 160, 30)];
    _titleLabel.textColor = [UIColor whiteColor];
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:_titleLabel];
    _titleLabel.alpha = 0;
   // _titleLabel.center = self.center;
}
- (UIView *)searchView {
    if (!_searchView) {
        _searchView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth - 160, 32)];
        _searchView.layer.cornerRadius = 7;
        _searchView.layer.borderColor = [UIColor whiteColor].CGColor;
        _searchView.layer.borderWidth = 1;
        _searchView.backgroundColor = [kContentColor colorWithAlphaComponent:0.2];
        
        
        UILabel *nameLabel = [[UILabel alloc] init];
        nameLabel.textColor = [UIColor whiteColor];
        nameLabel.text = MYLocalizedString(@"HOMEPAGE_SEARCH", nil);
        nameLabel.font = Font(14);
        [_searchView addSubview:nameLabel];
        
        [nameLabel sizeToFit];
        
        [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.centerY.equalTo(_searchView.mas_centerY);
            make.centerX.equalTo(_searchView.mas_centerX);
        }];
        
        UIImageView * searchIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"searchIcon"]];
        
        [_searchView addSubview:searchIcon];
        
        [searchIcon mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.right.equalTo(nameLabel.mas_left).offset(-5);
            make.centerY.equalTo(nameLabel.mas_centerY);
            make.width.mas_equalTo(20);
            make.height.mas_equalTo(20);
        }];
        
        
//        UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 21)];
        
        
//        
//        [view addSubview:searchIcon];
//        [view addSubview:nameLabel];
//        view.center = _searchView.center;
//        [_searchView addSubview:view];
        
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClicked)];
        
        [_searchView addGestureRecognizer:tap];
    }
    return _searchView;
}


- (void)tapClicked {
    if (_delegate && [_delegate respondsToSelector:@selector(goSearch)]) {
        [_delegate goSearch];
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
